package com.lizhen.j2eepattern.servicelocatorpattern;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Cache {
    private List<Service> services = new ArrayList();

    public Cache() {
    }

    public Service getService(String serviceName) {
        Iterator var3 = this.services.iterator();

        while (var3.hasNext()) {
            Service service = (Service) var3.next();
            if (service.getName().equalsIgnoreCase(serviceName)) {
                System.out.println("Returning cached  " + serviceName + " object");
                return service;
            }
        }

        return null;
    }

    public void addService(Service newService) {
        boolean exists = false;
        Iterator var4 = this.services.iterator();

        while (var4.hasNext()) {
            Service service = (Service) var4.next();
            if (service.getName().equalsIgnoreCase(newService.getName())) {
                exists = true;
            }
        }

        if (!exists) {
            this.services.add(newService);
        }

    }
}
